<template>
  <div class="login-bg">
    <div class="login-left">
      <div class="left-text">
        <h1>上海闻峥后台管理系统</h1>
        <p>Shanghai Wenzheng background management system</p>
      </div>
      <el-tooltip class="item" effect="dark" content="打开/关闭背景视频" placement="bottom-end" > <div class="video-icon" @click="videoFlag = !videoFlag">
          <i class="iconfont" :class=" !videoFlag ? 'icon-videoshipin' : 'icon-videooffshipinguanbi' " ></i>
        </div>
      </el-tooltip>

      <video
        v-show="!videoFlag"
        class="animated fadeIn"
        preload
        muted
        autoplay
        loop
        src="https://wlxtypero.oss-cn-beijing.aliyuncs.com/img/video.mp4"
      ></video>
      <img
        v-show="videoFlag"
        class="animated fadeIn"
        src="@/assets/images/logn-bg.jpg"
        alt="背景"
      />
    </div>
    <div class="login-container">
      <div class="login-box animated fadeInDown">
        <h1>Administrator Login</h1>
        <div class="login-from">
          <div class="item">
            <svg-icon icon-class="user" />
            <input
              v-model="loginForm.username"
              type="text"
              ref="username"
              ripple
              placeholder="UserName"
              
            />
          </div>
          <div class="item">
            <svg-icon icon-class="password" />
            <input
              v-model="loginForm.password"
              ref="password"
              :type="passwordType"
              ripple
              placeholder="Password 随便输"
            />
            <svg-icon
              @click="showPwd"
              class-name="eye-icon"
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </div>
          <div ripple class="flex-alc mybtn" @click="loginBtn">
            <div class="icon" v-if="loading">
              <i class="iconfont icon-Loading"></i>
            </div>
            <span>Login In</span>
          </div>
        </div>
      </div>
    </div>
    <div class="power">
      技术支持：<a href="http://www.willmid.com" target="_blank">上海闻峥</a>
    </div>
    <div class="power-logo">
      <a href="http://www.willmid.com" target="_blank">
        <img src="@/assets/images/logo.png" alt="闻峥" />
      </a>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "",
      },
      loading: false,
      redirect: undefined,
      videoFlag: localStorage.getItem("videoFlag") === "true",
      passwordType: "password",
      otherQuery: {},
    };
  },
  watch: {
    videoFlag(val) {
      localStorage.setItem("videoFlag", val);
    },
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
     // 自动聚焦
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    loginBtn() {
      let { username, password } = this.loginForm;
      if (!username || !password) {
        this.$message.error("用户名/密码不能为空");
        return false;
      }
      this.loading = true;
      this.$store
        .dispatch("user/login", this.loginForm)
        .then(() => {
          setTimeout(() => {
            this.$router.push({
              path: this.redirect || "/",
              query: this.otherQuery,
            });
            this.loading = false;
          }, 1000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss" scoped>
$bg: #fff;
$dark_gray: #889aa4;
$light_gray: #eee;
input:-webkit-autofill {
  box-shadow: 0 0 0px 1000px #f9f9fb inset !important;
}
@import "@/assets/css/main";

.login-bg {
  @include flex-btn();
  @include flex-alc();
  overflow: hidden;
  height: 100%;
  width: 100%;
  .login-left {
    width: 60%;
    flex-shrink: 0;
    position: relative;
    height: 100%;
    .video-icon {
      position: absolute;
      right: 40px;
      top: 40px;
      cursor: pointer;
      .iconfont {
        font-size: 20px;
        color: #fff;
      }
    }
    video {
      width: 100vw;
      display: block;
      position: absolute;
      z-index: -1;
      left: -40%;
    }
    img {
      display: block;
      width: 100vw;
      position: absolute;
      left: -40%;
      z-index: -2;
    }
    .left-text {
      position: absolute;
      top: 100px;
      left: 40px;
      z-index: 2;
      color: #fff;
    }
  }
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
    background: #ffffff;
    height: 100vh;
    position: relative;
    box-shadow: 0px 8px 57.12px 10.88px rgba(0, 0, 0, 0.53);
    .login-box {
      width: 70%;
      h1 {
        color: #67656d;
        font-size: 40px;
        text-align: left;
        font-family: "Arial Hebrew Scholar";
        margin-bottom: 100px;
      }
      .login-from {
        .item {
          margin-bottom: 30px;
          position: relative;
          .svg-icon {
            position: absolute;
            z-index: 2;
            top: 50%;
            left: 15px;
            transform: translateY(-50%);
            color: #999999;
            &.eye-icon {
              cursor: pointer;
              left: auto;
              right: 40px;
              position: absolute;
            }
          }

          input {
            height: 60px;
            line-height: 50px;
            padding: 10px 40px;
            background: #f9f9fb;
            //color: #999999;
            border-radius: 10px;
            width: 100%;
            transition: 0.5s;
            outline: none;
            box-sizing: border-box;
            border: 1px solid #f9f9fb;
            &::-webkit-input-placeholder {
              color: #dfdfde;
            }
            &:focus {
              border-radius: 10px;
              border: 1px solid #409eff;
            }
          }
        }
        .mybtn {
          background: #5867de;
          color: #ffffff;
          line-height: 50px;
          padding: 0 100px;
          border-radius: 100px;
          border: none;
          cursor: pointer;
          transition: 0.3s;
          margin-top: 40px;
          box-shadow: 0px 3px 10px 2px rgba(150, 162, 255, 0.52);
          opacity: 0.8;
          outline: none;
          display: inline-block;
          @keyframes rotate {
            from {
              transform: rotate(0deg);
            }
            to {
              transform: rotate(359deg);
            }
          }
          .icon {
            animation: rotate 1.5s linear infinite;
            margin-right: 10px;
            display: inline-block;
          }
          &:hover {
            opacity: 1;
            box-shadow: 0px 3px 10px 2px rgba(150, 162, 255, 0.52);
          }
        }
      }
    }
  }
  .power {
    position: absolute;
    bottom: 30px;
    right: 20px;
    font-size: 14px;
    width: 40%;
    text-align: center;
    color: #999;
    a {
      color: #409eff;
    }
  }
  .power-logo {
    position: absolute;
    right: 20px;
    width: 120px;
    top: 30px;
    img {
      display: block;
      width: 100%;
    }
  }
}
</style>
